package cn.iocoder.yudao.module.train.controller.admin.courseclasses;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.service.courseclasses.CourseClassesService;

@Tag(name = "管理后台 - 班级")
@RestController
@RequestMapping("/train/course-classes")
@Validated
public class CourseClassesController {

    @Resource
    private CourseClassesService courseClassesService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    @PreAuthorize("@ss.hasPermission('train:course-classes:create')")
    public CommonResult<Long> createCourseClasses(@Valid @RequestBody CourseClassesSaveReqVO createReqVO) {
        return success(courseClassesService.createCourseClasses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级")
    @PreAuthorize("@ss.hasPermission('train:course-classes:update')")
    public CommonResult<Boolean> updateCourseClasses(@Valid @RequestBody CourseClassesSaveReqVO updateReqVO) {
        courseClassesService.updateCourseClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:course-classes:delete')")
    public CommonResult<Boolean> deleteCourseClasses(@RequestParam("id") Long id) {
        courseClassesService.deleteCourseClasses(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级")
                @PreAuthorize("@ss.hasPermission('train:course-classes:delete')")
    public CommonResult<Boolean> deleteCourseClassesList(@RequestParam("ids") List<Long> ids) {
        courseClassesService.deleteCourseClassesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:course-classes:query')")
    public CommonResult<CourseClassesRespVO> getCourseClasses(@RequestParam("id") Long id) {
        CourseClassesCommodityDTO courseClasses = courseClassesService.getCourseClasses(id);
        return success(BeanUtils.toBean(courseClasses, CourseClassesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级分页")
    @PreAuthorize("@ss.hasPermission('train:course-classes:query')")
    public CommonResult<PageResult<CourseClassesRespVO>> getCourseClassesPage(@Valid CourseClassesPageReqVO pageReqVO) {
        PageResult<CourseClassesCommodityDTO> pageResult = courseClassesService.getCourseClassesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseClassesRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得全部班级")
    @PreAuthorize("@ss.hasPermission('train:course-classes:query')")
    public CommonResult<List<CourseClassesRespVO>> getCourseClassesList(@Valid CourseClassesPageReqVO pageReqVO) {
        List<CourseClassesCommodityDTO> pageResult = courseClassesService.getCourseClassesList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseClassesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级 Excel")
    @PreAuthorize("@ss.hasPermission('train:course-classes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseClassesExcel(@Valid CourseClassesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseClassesCommodityDTO> list = courseClassesService.getCourseClassesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级.xls", "数据", CourseClassesRespVO.class,
                        BeanUtils.toBean(list, CourseClassesRespVO.class));
    }

}
